//
//  HNSH_FavoriteListViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/4.
//

#import "HNSH_FavoriteListViewController.h"

//view
#import "HNSH_HomeGoodsCell.h"

//vc
#import "HNSH_GoodsDetailViewController.h"

@interface HNSH_FavoriteListViewController ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

/* listAry */
@property (nonatomic, strong) NSMutableArray *listAry;

@end

static NSString *HNSH_HomeGoodsCellID = @"HNSH_HomeGoodsCellID";

@implementation HNSH_FavoriteListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showNavTitle:@"我的收藏" showLeftItem:YES];
    
    [self configUI];
    
    [self loadGoodsFavoriteList];
}

- (void)configUI {
    
    [self configCollectionView];
}

- (void)configCollectionView {
    self.collectionView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
    
    [self.collectionView registerClass:[HNSH_HomeGoodsCell class] forCellWithReuseIdentifier:HNSH_HomeGoodsCellID];
    
    self.collectionView.emptyDataSetSource = self;
    self.collectionView.emptyDataSetDelegate = self;
}

#pragma mark networking
- (void)loadGoodsFavoriteList {
    [HNSH_ShopNetServer requestGoodsFavoriteListSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [self.listAry removeAllObjects];
            NSArray *ary = [NSArray yy_modelArrayWithClass:[HNSH_GoodsListModel class] json:responseObject.resultset[@"list"]];
            [self.listAry addObjectsFromArray:ary];
            [self.collectionView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark lazy
- (NSMutableArray *)listAry {
    if (!_listAry) {
        _listAry = [NSMutableArray arrayWithCapacity:0];
    }
    return _listAry;
}

#pragma mark - 空数据处理 DZNEmptyDataSetSource, DZNEmptyDataSetDelegate
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"empty_list_data"];
}

- (BOOL)emptyDataSetShouldFadeIn:(UIScrollView *)scrollView{
    return YES;
}

#pragma mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.listAry.count;
}
//设置海报图片
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HNSH_HomeGoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HNSH_HomeGoodsCellID forIndexPath:indexPath];
    cell.model = self.listAry[indexPath.row];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *reusableView = [collectionView supplementaryViewForElementKind:NSStringFromClass(UICollectionReusableView.class) atIndexPath:indexPath];
    return reusableView;
}
#pragma mark UICollectionViewDelegateFlowLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(20, 20, 20, 20);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = (kScreenWidth-53)/2;
    return CGSizeMake(width, width+80);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 20;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 13;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeZero;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeZero;
}
#pragma mark UICollectionViewDataSource
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HNSH_GoodsListModel *model = self.listAry[indexPath.row];
    HNSH_GoodsDetailViewController *vc = [[HNSH_GoodsDetailViewController alloc]init];
    vc.goodsId = model.uid;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
